package com.next.muscu.data;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * <b>Exotype est la classe repr�sentant un type d'exercice</b>
 * @author sebastien
 * @version 0.1
 */
@Entity
@Table(name = "exotype", catalog = "jaydb", schema = "")
public class ExoTypeExo implements Comparable<ExoTypeExo>,  MyDataObject, java.io.Serializable {

	public static final String PP_NAME = "name";
	public static final String PP_CODE = "code";
	private static final String PP_ID = "id";

	/**
	 * id du type d'exercice
	 * @see ExoTypeExo#getId()
	 * @see  ExoTypeExo#setId()
	 */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
	@Column(name = "ID")
	private Integer id;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		Integer old = this.id;
		this.id = id;
		ppChangeSupport.firePropertyChange(PP_ID, old, this.id );
	}
	/**
	 * Nom du type d'exercice
	 * @see ExoTypeExo#getName()
	 * @see  ExoTypeExo#setName()
	 */
        @Column(name="NAME")
	private String name;
	
	/**
	 * Code du type d'exerice, cl�s d'acc�s � celui-ci 
	 * @see ExoTypeExo#getCode()
	 * @see  ExoTypeExo#setCode()
	 */
        @Column(name="CODE")
	private String code;

        @OneToMany
        @JoinColumn(name="ID_EXOTYPE", nullable=false)
	private Set exos = new HashSet(0);
	

	public Set getExos() {
		return exos;
	}
	public void setExos(Set exos) {
		this.exos = exos;
	}
	@Transient
	PropertyChangeSupport ppChangeSupport = new PropertyChangeSupport(this);
	/**
     * Constructeur ExoType.
     * <p>
     * 2 contructeurs dispo un avec param et un sans pour la contruction d'objet nouveau
     * </p>
     * 
     * @param code
     *            L'identifiant unique de l exoType.
     * @param name
     *            Le nom du type d'exo
     * 
     * @see Zero#code
     * @see Zero#name
     */
	public ExoTypeExo(Integer id,String code, String name) {
		super();
		this.name = name;
		this.code = code;
		this.id = id;
	}
	/**
     * Constructeur ExoType.
     * <p>
     * 2 contructeurs dispo un avec param et un sans pour la contruction d'objet nouveau
     * </p>
     */
	public ExoTypeExo ()
	{
		this.name = new String();
		this.code = new String();
	}
	
	@Override
	public String toString() {
		return "ExoType [id=" +id +", name=" + name + ", code=" + code + "]";
	}
	
	/**
	 * Retourne le nom de l'ExoType.
	 * @return  nom de l'ExoType.
	 * @uml.property  name="name"
	 */

	public String getName() {
		return name;
	}
	
	/**
	 * met � jour le nom du type d'exo
	 * @param name    Nom du type d'exo.
	 * @uml.property  name="name"
	 */
	public void setName(String name) {
		String old = this.name;
		this.name = name;
		ppChangeSupport.firePropertyChange(PP_NAME, old, this.name );
	}
	
	/**
	 * Retourne l'identifiant unique de l'ExoType.
	 * @return  id de l'exoType
	 * @uml.property  name="code"
	 */
	public String getCode() {
		return code;
	}
	
	/**
	 * met � jour l'identifiant unique du type d'exo
	 * @param code    identifiant unique du type d'exo.
	 * @uml.property  name="code"
	 */
	public void setCode(String code) {
		String old = code;
		this.code = code;
		ppChangeSupport.firePropertyChange(PP_CODE, old, this.name );
	}
	@Override
	 public boolean equals(Object o) {
	        if (!(o instanceof ExoTypeExo))
	            return false;
	        ExoTypeExo e = (ExoTypeExo)o;
	        return e.getId().equals(id);
	    }
	@Override
	    public int hashCode() {
	        return id.hashCode();
	    }

	    public int compareTo(ExoTypeExo e) {
	        return this.getId().compareTo(e.getId());
	    }
		public void addPropertyChangeListener(PropertyChangeListener listener) {
			ppChangeSupport.addPropertyChangeListener(listener);
		}
		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			ppChangeSupport.addPropertyChangeListener(propertyName, listener);
		}
		public PropertyChangeListener[] getPropertyChangeListeners() {
			return ppChangeSupport.getPropertyChangeListeners();
		}
		public PropertyChangeListener[] getPropertyChangeListeners(
				String propertyName) {
			return ppChangeSupport.getPropertyChangeListeners(propertyName);
		}
		public void removePropertyChangeListener(PropertyChangeListener listener) {
			ppChangeSupport.removePropertyChangeListener(listener);
		}
		public void removePropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			ppChangeSupport
					.removePropertyChangeListener(propertyName, listener);
		}
		public boolean copy(MyDataObject o) {
	        if (!(o instanceof ExoTypeExo))
	            return false;
	        ExoTypeExo e = (ExoTypeExo)o;
	        this.id = e.getId();
	        this.code = e.getCode();
	        this.name = e.getName();
	        return true;
		}
		@Override
		public void Create() {
			code = "ZZ";
			name = "nouveau";
		}  
	    
}
